package org.syntaxlisp.core;

import org.apache.commons.lang3.StringUtils;

/**
 * @author HuangYijun
 * @data 2023-01-30 11:04
 **/
public class SyntaxLispParseUtils {

    public static boolean isLetter (char ch) {
        return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
    }

    public static int toDigitVal (char digitCh) {
        int val = digitCh - '0';
        if (val >= 0 && val <= 9) {
            return val;
        } else {
            throw new ParseException("No DIGIT character can not parse to number value.");
        }
    }

    public static boolean checkIsValidId (String id) {
        if (!StringUtils.isEmpty(id) && isLetter(id.charAt(0))) {
            for (char idCh : id.toCharArray()) {
                if (!isLetter(idCh) && !isDigit(idCh) && idCh != '_') {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }

    public static boolean isDigit (char ch) {
        return ch >= '0' && ch <= '9';
    }
}
